//
//  AnnounceListView.m
//  AppleMarket
//
//  Created by yangy on 31/10/2018.
//  Copyright © 2018 szgsy. All rights reserved.
//

#import "AnnounceListView.h"
// viewModel
#import "AnnounceViewModel.h"
// model
#import "NewsModel.h"

@interface AnnounceListView()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) AnnounceViewModel *viewModel;
@end

@implementation AnnounceListView

- (instancetype)initWithFrame:(CGRect)frame viewModel:(id) viewModel
{
    self = [super initWithFrame:frame];
    if(self){
        _viewModel = viewModel;
        [self addSubview:self.tableView];
        [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.mas_equalTo(UIEdgeInsetsMake(0, 0, 0, 0));
        }];
        [self bindViewModel];
    }
    return self;
}

- (void) bindViewModel
{
    //[self.viewModel.refershAnnounceCommand execute:nil];
    @weakify(self);
    [self.viewModel.refershEndSubject subscribeNext:^(id  _Nullable x) {
        @strongify(self);
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        [self.tableView reloadData];
    }];
}

#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.viewModel.refershDatas.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *idx = @"UITableViewCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:idx];
    if(cell == nil){
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:idx];
        cell.textLabel.font = kFont(15);
        cell.detailTextLabel.font = kFont(12);
        cell.textLabel.textColor = kColorWithHex(0x0E304C);
        cell.detailTextLabel.textColor = kColorWithHex(0x808C99);
    }
    
    NewsModel *m = self.viewModel.refershDatas[indexPath.row];
    cell.textLabel.text = m.DocTitle;
    cell.detailTextLabel.text = m.CreateTime;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 82;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NewsModel *m = self.viewModel.refershDatas[indexPath.row];
    [self.viewModel.detailClickSubject sendNext:m];
}

#pragma mark - lazy load
- (UITableView *)tableView
{
    if(!_tableView){
        _tableView = [[UITableView alloc] initWithFrame:self.bounds style:UITableViewStylePlain];
        [_tableView setSeparatorColor:kColorWithHex(0xEFEFEF)];
        [_tableView setSeparatorInset:UIEdgeInsetsMake(0, 15, 0, 15)];
        [_tableView setDelegate:self];
        [_tableView setDataSource:self];
        [_tableView setTableFooterView:[UIView new]];
        [_tableView setMj_header:[MJRefreshNormalHeader headerWithRefreshingBlock:^{
            [self.viewModel.refershAnnounceCommand execute:nil];
        }]];
        [_tableView setMj_footer:[MJRefreshAutoFooter footerWithRefreshingBlock:^{
            [self.viewModel.refershMoreAnnounceCommand execute:nil];
        }]];
        [_tableView.mj_header beginRefreshing];
    }
    return _tableView;
}

@end
